﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models.DTO;
using tracker.Models;
using tracker.Models.BLL;

namespace tracker.Controllers
{
    public class HandlerChangePasswordController : Controller
    {
        //
        // GET: /HandlerChangePassword/
        [CallAjaxFilter]
        public JsonStringResult Index(string uid, string type, string password, string oldpassword)
        {
            String result = "";
            EUserLogin currentUser = new EUserLoginBLL().getByUserName(Session[Common.USER].ToString());
            String currentSecurityCode = Session[Common.USER_SECUTIRY_CODE].ToString();
            if (uid == currentSecurityCode)
            {
                //update
                result = new EUserLoginBLL().changePassword(uid, oldpassword, password);
            }
            else
            {
                String currentUserGroupID = Session[Common.USER_GROUPID].ToString();
                if (currentUserGroupID == "3")
                {
                    //Update
                    result = new EUserLoginBLL().changePassword(uid, oldpassword, password);
                }
                else if (currentSecurityCode == "4")
                {
                    // Check user
                    EUserLogin updUser = new EUserLoginBLL().getBySecurityCode(uid);
                    if (updUser != null && updUser.ParentCode == currentSecurityCode)
                    {
                        //update
                       result = new EUserLoginBLL().changePassword(uid, oldpassword, password);
                    }
                    else
                    {
                        //Error
                        result = "1";
                    }
                }
            }
            if (result == "0")
            {
                result = Utilities.createJSON(Common.SUCCESS, Common.TRUE);
            }
            else
            {
                result = Utilities.createJSON(Common.SUCCESS, Common.IS_ERROR);
            }
            return new JsonStringResult(result);
        }

    }
}
